package dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import model.AdminMessage;
import model.GeneralMessage;

/**
 * Session Bean implementation class MailDao
 * 
 * @author zirbom
 */
@Stateless
@LocalBean
public class MailDao {

	@PersistenceContext
	EntityManager em;
	

	/**
	 * Default constructor.
	 */
	public MailDao() {

		// TODO Auto-generated constructor stub
	}

	/**
	 * returneaza Lista de mail-uri din AdminMessage
	 * 
	 * @return mailList
	 * @author zirbom
	 */
	public List<AdminMessage> getAdminMailList() {

		List<AdminMessage> finalMailList = new ArrayList<AdminMessage>();
		List<AdminMessage> mailList = em.createQuery("SELECT m FROM AdminMessage m ", AdminMessage.class).getResultList();

		for (AdminMessage mail : mailList) {
			if (!(mail instanceof model.GeneralMessage))
				finalMailList.add(mail);
		}

		return finalMailList;

	}

	/**
	 * adauga un general message in baza de date
	 * 
	 * @author fodorl
	 * @param generalMessage
	 */

	public void insertGeneralMessage(GeneralMessage generalMessage) {

		em.persist(generalMessage);
	}

	/**
	 * adauga un admin message in baza de date
	 * 
	 * @author fodorl
	 * @param adminMessage
	 */

	public void insertAdminMessage(AdminMessage adminMessage) {

		em.persist(adminMessage);
	}

	/**
	 * @author zirbom
	 * @param id
	 * @return lista de mail-uri pentu supervisorul care are id-ul din signatura
	 *         metodei
	 */

	public List<GeneralMessage> getUserMailList(int idUser, int idRepository) throws SQLException {

		List<GeneralMessage> mailList = new ArrayList<GeneralMessage>();

		mailList = em.createQuery("SELECT m FROM GeneralMessage m where m.folder.id=:param1", GeneralMessage.class).setParameter("param1", idRepository).getResultList();

		return mailList;
	}

	/**
	 * @author fodorl
	 * @param GeneralMessage
	 * @return boolean returneaza true daca mai exista un GeneralMessage cu
	 *         aceste date in baza de date
	 */

	public boolean existaGeneralMessage(GeneralMessage generalMessage) {

		boolean exists = false;
		List<GeneralMessage> generalMessages = em.createQuery("SELECT m FROM GeneralMessage m where m.sender=?1", GeneralMessage.class).setParameter(1, generalMessage.getSender()).getResultList();
		for (GeneralMessage message : generalMessages) {
			exists = equalGeneralMessages(message, generalMessage);
			if (exists) {
				break;
			}
		}
		return exists;
	}

	/**
	 * @author fodorl
	 * @param adminMessage
	 * @return boolean returneaza true daca mai exista un AdminMessage cu aceste
	 *         date in baza de date
	 */

	public boolean existaAdminMessage(AdminMessage adminMessage) {

		boolean exists = false;
		List<AdminMessage> adminMessages = em.createQuery("SELECT m FROM AdminMessage m where m.sender=?1", AdminMessage.class).setParameter(1, adminMessage.getSender()).getResultList();
		for (AdminMessage message : adminMessages) {
			exists = equalAdminMessages(message, adminMessage);
			if (exists) {
				break;
			}
		}
		return exists;
	}

	/**
	 * @author fodorl
	 * @param message1
	 * @param message2
	 * @return boolean returneaza true daca 2 GeneralMessage sunt identice,
	 *         false altfel
	 */

	public boolean equalGeneralMessages(GeneralMessage message1, GeneralMessage message2) {

		if ((message1.getFolder().equals(message2.getFolder())) && (message1.getMessageType().equals(message2.getMessageType()))&&(message1.getContent().equals(message2.getContent()))) {
			return true;
		} else
			return false;
	}

	/**
	 * @author fodorl
	 * @param message1
	 * @param message2
	 * @return boolean returneaza true daca 2 AdminMessage sunt identice, false
	 *         altfel
	 */

	public boolean equalAdminMessages(AdminMessage message1, AdminMessage message2) {

		if (message1.getMessageType().equals(message2.getMessageType())&&(message1.getContent().equals(message2.getContent()))) {
			return true;
		} else
			return false;
	}

	public void deleteGMEntry(int id) {

		
		
		GeneralMessage entry = em.find(GeneralMessage.class, id);
		if(entry!=null){
			em.remove(em.merge(entry));
		}
		
		
		// TODO Auto-generated method stub

	}

	public void deleteAMEntry(int id) {

		
		
		AdminMessage entry = em.find(AdminMessage.class, id);
		if(entry!=null){
			em.remove(em.merge(entry));
		}
		

		
		// TODO Auto-generated method stub

	}

	public List<GeneralMessage> getStatusRequests(int idUser, int idRepository) throws SQLException {

		List<GeneralMessage> supListMail;
		
		supListMail = em.createQuery("SELECT m from GeneralMessage m where m.sender.id=:param", GeneralMessage.class)
				.setParameter("param", idUser)
				.getResultList();
		
		// TODO Auto-generated method stub
		return supListMail;
	}

	public List<AdminMessage> getForAdminEntries(int idUser, int idRepository) {
	
		List<AdminMessage> adminEntries = em.createQuery("SELECT c FROM AdminMessage c where c.sender.id=:param1", AdminMessage.class)
				.setParameter("param1", idUser)
				.getResultList();
		
		return adminEntries;
	}
}
